package com.futurelight.recipes.adapter;

import android.app.Activity;
import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.futurelight.recipes.ui.fragment.RecipesInfoItemFragment;
import com.futurelight.recipes.model.ItemViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecipesItemAdapter extends FragmentStateAdapter {
    private FragmentActivity activity;
    private Fragment fragment;

    private List<Fragment> fragmentsList = new ArrayList<>();

    public RecipesItemAdapter(@NonNull Fragment fragment, FragmentActivity activity) {
        super(fragment);
        this.activity = activity;
        this.fragment = fragment;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        if (fragmentsList.size() > position && fragmentsList.get(position) != null) {
            return fragmentsList.get(position);
        } else {
            RecipesInfoItemFragment recipesInfoItemFragment = RecipesInfoItemFragment.getInstance(activity,position);
            fragmentsList.add(recipesInfoItemFragment);
            return recipesInfoItemFragment;
        }
    }

    @Override
    public int getItemCount() {
        return ItemViewModel.types.size();
    }
}
